package com.itheima.reggie.controller;

import com.aliyuncs.utils.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * 套餐控制器
 */
@Controller
@ResponseBody
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;
    @GetMapping(value = "/page",name = "分页查询套餐")
    public ResultInfo findByPage(@RequestParam(value = "page",defaultValue = "1") Integer pageNum,
                                 @RequestParam(defaultValue = "5") Integer pageSize, String name){
        //调用套餐业务层
        Page<Setmeal> page = setmealService.findByPage(pageNum,pageSize,name);
        //返回结果
        return ResultInfo.success(page);
    }

    @CacheEvict(value = "setmeal",key = "#setmeal.categoryId")
    @PostMapping(name = "添加套餐")
    public ResultInfo save(@RequestBody Setmeal setmeal){
        setmealService.save(setmeal);
        return ResultInfo.success(null);
    }

    @GetMapping(value = "/{id}",name = "根据id查询套餐")
    public ResultInfo findById(@PathVariable("id") Long id){
        Setmeal setmeal = setmealService.findById(id);
        return ResultInfo.success(setmeal);
    }

    @CacheEvict(value = "setmeal",key = "#setmeal.categoryId")
    @PutMapping(name = "套餐的修改")
    public ResultInfo update(@RequestBody Setmeal setmeal){
        setmealService.update(setmeal);
        return ResultInfo.success(null);
    }

    @CacheEvict(value = "setmeal",allEntries = true)
    @PostMapping(value = "/status/{status}",name = "批量停售")
    public ResultInfo updateStatus(@PathVariable("status") Integer status,Long[] ids){
        setmealService.updateStatus(status,ids);
        return ResultInfo.success(null);
    }

    @CacheEvict(value = "setmeal",allEntries = true)
    @DeleteMapping(name = "删除套餐")
    public ResultInfo delete(Long[] ids){
        String messAge = setmealService.deleteById(ids);
        if (StringUtils.isNotEmpty(messAge)){
            return ResultInfo.error("您选择的套餐删除失败:"+messAge);
        }
        return ResultInfo.success(null);
    }


}
